package com.luo.chengrui.module.crbpm.controller.admin.flowable;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.luo.chengrui.module.crbpm.controller.admin.flowable.vo.*;
import com.luo.chengrui.module.crbpm.dal.dataobject.flowable.ActReModelDO;
import com.luo.chengrui.module.crbpm.convert.flowable.ActReModelConvert;
import com.luo.chengrui.module.crbpm.service.flowable.ActReModelService;

@Tag(name = "管理后台 - 流程模型")
@RestController
@RequestMapping("/crbpm/act-re-model")
@Validated
public class ActReModelController {

    @Resource
    private ActReModelService actReModelService;

    @PutMapping("/update")
    @Operation(summary = "更新流程模型")
    @PreAuthorize("@ss.hasPermission('crbpm:act-re-model:update')")
    public CommonResult<Boolean> updateActReModel(@Valid @RequestBody ActReModelUpdateReqVO updateReqVO) {
        actReModelService.updateActReModel(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得流程模型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crbpm:act-re-model:query')")
    public CommonResult<ActReModelRespVO> getActReModel(@RequestParam("id") String id) {
        ActReModelDO actReModel = actReModelService.getActReModel(id);
        return success(ActReModelConvert.INSTANCE.convert(actReModel));
    }

    @GetMapping("/list")
    @Operation(summary = "获得流程模型列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('crbpm:act-re-model:query')")
    public CommonResult<List<ActReModelRespVO>> getActReModelList(@RequestParam("ids") Collection<String> ids) {
        List<ActReModelDO> list = actReModelService.getActReModelList(ids);
        return success(ActReModelConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得流程模型分页")
    @PreAuthorize("@ss.hasPermission('crbpm:act-re-model:query')")
    public CommonResult<PageResult<ActReModelRespVO>> getActReModelPage(@Valid ActReModelPageReqVO pageVO) {
        PageResult<ActReModelDO> pageResult = actReModelService.getActReModelPage(pageVO);
        return success(ActReModelConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出流程模型 Excel")
    @PreAuthorize("@ss.hasPermission('crbpm:act-re-model:export')")
    @OperateLog(type = EXPORT)
    public void exportActReModelExcel(@Valid ActReModelExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ActReModelDO> list = actReModelService.getActReModelList(exportReqVO);
        // 导出 Excel
        List<ActReModelExcelVO> datas = ActReModelConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "流程模型.xls", "数据", ActReModelExcelVO.class, datas);
    }

}
